package cn.itcast.bos.service.base.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.FixedAreaRepository;
import cn.itcast.bos.dao.base.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.FixedAreaService;
@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {

	@Autowired
	private FixedAreaRepository fixedAreaRepository;
	@Override
	public void save(FixedArea model) {
         fixedAreaRepository.save(model);
	}
	@Override
	public Page<FixedArea> fixedArea_pageQuery(
			Specification<FixedArea> specification, Pageable pageable) {
		return fixedAreaRepository.findAll(specification, pageable);
	}
	@Override
	public void updateTag(String tag, String ids) {
		String[] idString = ids.split(",");
		Character log=null;
		if (tag.equals("1")) {
			log=tag.charAt(0);
		}
		for (String id : idString) {
			fixedAreaRepository.updateTag(log,id);
		}
	}
	
	
	@Autowired
	private CourierRepository courierRepository;
	@Autowired
	private TakeTimeRepository takeTimeRepository;
	
	@Override
	public void associationCourierToFixedArea(FixedArea model,
			Integer courierId, Integer takeTimeId) {
		
		
		//先把瞬时态的FixedArea,courier,takeTime变成持久态
		FixedArea fixedArea = fixedAreaRepository.findOne(model.getId());
		Courier courier = courierRepository.findOne(courierId);
		TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
		//将快递员关联到定区，由定区添加，因为外键由定区管理
		fixedArea.getCouriers().add(courier);
		//为快递进行排班
		courier.setTakeTime(takeTime);
		
	}
	@Override
	public FixedArea findFixedAreaById(String stringCellValue) {
		return fixedAreaRepository.findOne(stringCellValue);
	}
	@Override
	public List<FixedArea> findAllFixedArea() {
		return fixedAreaRepository.findAll();
	}

}
